<?php
    
    define( 'PAGE_LIMIT', 10 );
    
    function Baboo_DbFetchTables( &$db ) { 
        try {
            $db->FetchTables();
        } catch ( Exception $e ) {
            if ( (int)$e->getCode() == 1044 ) {
                return false;
            }
            else {
                throw $e;
            }
        }
        return true;
    }

    function Baboo_Init( $serverSelected, $dbSelected, $tableSelected ) {
        global $server;
        global $db;
        global $table;
        global $rabbit_settings;
        global $config;

        $config = include_once "config.php";
        if ( !is_array( $config ) || empty( $config ) ) {
            die( "You need to specify at least one server! Please check the config.php file on the root directory" );
        }

        if ( !empty( $serverSelected ) ) {
            if ( !isset( $config[ $serverSelected ] ) ) {
                die( "The server specified is not in your configuration file" );
            }
            $server = $config[ $serverSelected ];
            $server[ 'name' ] = $serverSelected;
        }
        else {
            $server_names = array_keys( $config );
            $server = $config[ $server_names[ 0 ] ];
            $server[ 'name' ] = $server_names[ 0 ];
        }

        if ( empty( $server[ 'address' ] ) ) {
            die( 'config.php: Please specify a valid address for the selected server.' );
        }
        if ( !empty( $_COOKIE[ 'baboo_user' ] ) ) {
            $info = $_COOKIE[ 'baboo_user' ];
            $username = substr( $info, 0, strpos( $info, ':' ) );
            $password = substr( $info, strpos( $info, ':' ) + 1 );
            $user_type = 'cookie_user';
        }
        else if ( empty( $server[ 'username' ] ) ) {
            $server[ 'loginrequired' ] = true;
            return;
        }
        else {
            $username = $server[ 'username' ];
            $password = $server[ 'password' ];
            $user_type = 'config_user';
        }
        $db = New BabooDatabase();
        $db->Connect( $server[ 'address' ] );
        $db->Authenticate( $username, $password );
        if ( $user_type == 'cookie_user' && !$db->TestConnect() ) {
            die( 'cookie failed' );
            $username = $server[ 'username' ];
            $password = $server[ 'password' ];
            $db->Authenticate( $username, $password );
            $user_type = 'config_user';
        }
        if ( $user_type == 'config_user' && !$db->TestConnect() ) {
            $using_password = ( !empty( $password ) ) ? 'YES' : 'NO';
            die( "Access denied for user $username (using password: $using_password)" );
        }
        $connected = false;
        if ( !empty( $dbSelected ) ) { // try connecting to the database specified by GET
            $db->SwitchDb( $dbSelected );
            $connected = Baboo_DbFetchTables( $db );
        }
        if ( !$connected && !empty( $server[ 'database' ] ) ) { // unable to connect; try connecting to default server, if it's not empty
            $db->SwitchDb( $server[ 'database' ] );
            $connected = Baboo_DbFetchTables( $db );
        }
        if ( !$connected ) { // still not connected; connect to the first available database of the server
            $db->SwitchToFirstDb();
            $connected = Baboo_DbFetchTables( $db );
        }

        $table = false;
        $tables = $db->Tables();

        if ( count( $tables ) == 0 ) {  // there are no tables
            $table = New DbTable();     // use a non-existing table
            return;                     // nothing left to do here
        }

        if ( !empty( $tableSelected ) ) {
            $table_name = $tableSelected;
            $table = isset( $tables[ $table_name ] ) ? $tables[ $table_name ] : false;
        }
        if ( $table == false ) { // $_GET[ 'table' ] is empty or invalid
            $table_names = array_keys( $tables );
            $table = $tables[ $table_names[ 0 ] ];
        }
    }

?>
